import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './vuex/store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './styles/reset.css'
import './styles/base.css'
import './styles/element.scss'
import { getUrlKey } from './util/util'
import { LoginResource} from './axios/index'
import { Decrypt, Encrypt} from './util/urlEncrypt'
import { Message } from 'element-ui';


Vue.config.productionTip = false
Vue.use(ElementUI, {size: "small"})
//全局守卫
router.beforeEach((to, from, next)=> {
  let index = window.location.href.indexOf('?');
  let routerList = router.options.routes
  let authorityList = store.getters.getUserInfo.uapUserInfoVo && store.getters.getUserInfo.uapUserInfoVo.authorityList ? store.getters.getUserInfo.uapUserInfoVo.authorityList : []
  // console.log(Encrypt('rybm=5329230004&&dwbm=532923'))
  if (index >= 0) {
    let urlData = Decrypt(window.location.href.substr(index + 1));
    LoginResource
    .userLogin({
      rybm: getUrlKey('rybm', window.location.href.substr(0, index + 1) + urlData),
      dwbm: getUrlKey('dwbm', window.location.href.substr(0, index + 1) + urlData)
    })
    .then(res => {
      let userInfo = res.data;
      userInfo.role = 2;
      store.dispatch('setUserInfo', userInfo);
      window.localStorage.setItem("saToken", res.data.saToken);
      authorityList = res.data.uapUserInfoVo && res.data.uapUserInfoVo.authorityList ? res.data.uapUserInfoVo.authorityList : []
      if(to.name === 'Login') {
        next({name: 'ClueMangeInquisitor'});
      }
      else {
        if (!routerList.some(item => item.name === to.name)) {
          next({name: '404'});
        }
        else if (!authorityList.some(item => item.url === to.path)) {
          next({name: '403'});
        } 
        else {
          next()
        }
      }
    })
  }
  else if (to.name === '403' || to.name === '404' || to.name === 'Login') {
    next()
  }
  else if(to.name === 'ManageList' || to.name === 'TaskList' || to.name === 'TaskDetails') {
    if(!window.localStorage.getItem('userInfo')) {
      Message.error('请重新登录')
      next({name: 'Login'})
    }
    else if(window.localStorage.getItem('userInfo') && JSON.parse(window.localStorage.getItem('userInfo')).role === 2) {
      next({name: 'ClueMangeInquisitor'});
    }
    else if (!routerList.some(item => item.name === to.name)) {
      next({name: '404'});
    }
    else if (!authorityList.some(item => item.url === to.path)) {
      next({name: '403'});
    } 
    else {
      next()
    }
  }
  else if(to.name !== 'Login' && (!window.localStorage.getItem('userInfo') || !JSON.parse(window.localStorage.getItem('userInfo')).rybm)) {
    Message.error('请重新登录')
    next({name: 'Login'})
  }
  else {
    if (!routerList.some(item => item.name === to.name)) {
      next({name: '404'});
    }
    else if (!authorityList.some(item => item.url === to.path)) {
      next({name: '403'});
    } 
    else {
      next()
    }
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
